package com.zhihesj.anan.security;

import com.zhihesj.anan.system.entity.UserAuths;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;

/**
 * @author Administrator
 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
public class SecurityUserDetails extends UserAuths implements UserDetails {

    private Collection<? extends GrantedAuthority> authorities;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    public String getPassword() {
        return this.getCredential();
    }

    @Override
    public String getUsername() {
        return this.getIdentifier();
    }

    /**
     * TODO 该处需要重写密码,不应该为123456
     */
    public SecurityUserDetails(String userName,String password, Collection<? extends GrantedAuthority> authorities){
        this.authorities = authorities;
        this.setIdentifier(userName);
        //下方语句是不通过加密，如果想要不加密，就不要注释掉下方语句
        //String encode = new BCryptPasswordEncoder().encode(password);
        this.setCredential(password);
        this.setAuthorities(authorities);
    }

    /**
     * 账户是否过期
     * @return
     */
    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    /**
     * 是否禁用
     * @return
     */
    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    /**
     * 密码是否过期
     * @return
     */
    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 是否启用
     * @return
     */
    @Override
    public boolean isEnabled() {
        return true;
    }
}